<?php
include_once 'ReadArticlePresenter.php';
include_once 'AbstractView.php';
include_once 'InsultBuilder.php';
class ReadArticleView extends AbstractView {
	private $presenter;
	private $articleId;
	private $article;
	public function __construct($articleId) {
		parent::__construct ();
		$this->articleId = $articleId;
		$this->presenter = new ReadArticlePresenter ( $this, parent::getContentRepository () );
		
		$this->article = parent::getContentRepository ()->getArticle ( $articleId );
	}
	public function getArticleId () {
		return $this->articleId;
	}
	public function postCommentFailed($reason) {
		$builder = new InsultBuilder ();
		$builder->setMessage ( $reason )->setTitle ( "Have you got a DNA problem?" )->setPicture ( "commentfailed.jpg" )->setSource ( "readarticle.php?article=" . $this->articleId );
		parent::redirect ( $builder->build () );
	}
	public function postCommentSucceed() {
		$builder = new InsultBuilder ();
		$builder->setMessage ( "Oh, boy... Another stupid comment." )->setTitle ( "Maybe not in DNA... Who knows?" )->setPicture ( "comment_succeed.jpg" )->setSource ( "readarticle.php?article=" . $this->articleId );
		parent::redirect ( $builder->build () );
	}
	public function deleteCommentSucceed() {
		$builder = new InsultBuilder ();
		$builder->setMessage ( "Kill for the name of justice." )->setTitle ( "Got it!" )->setPicture ( "deletecomment.jpg" )->setSource ( "readarticle.php?article=" . $this->articleId );
		parent::redirect ( $builder->build () );
	}
	public function render() {
		if (isset ( $_GET ["deleteComment"] ) && parent::invokedByAdmin ()) {
			
			$this->presenter->deleteComment ( $_GET ["deleteComment"] );
			
			return;
		}
		
		if ($this->article == NULL) {
			$builder = new InsultBuilder ();
			$builder->setMessage ( "Only steers and queers plays with URL, and you don't look much like a steer to me, so that kinda narrows it down!" )->setTitle ( "Like playing with URL?" )->setPicture ( "userprofile_user_does_not_exist.jpg" )->setSource ( "index.php" );
			parent::redirect ( $builder->build () );
			 
			return;
		}
		
		
		if (isset ( $_POST ["comment"] ) && (parent::invokedByUser () || parent::invokedByAdmin ())) {
			
			$comment = Comment::newInstance ( $_POST ["comment"], parent::getActiveUser ()->getUserName (), $this->article->getArticleId () );
			$this->presenter->postComment ( $comment );
			return;
		}
		
		require 'readArticle.phtml';
		
	}
	public function renderComments() {
		$comments = $this->article->getComments ();
		foreach ( $comments as $comment ) {
			if (parent::invokedByAdmin ()) {
				echo '<a href="readarticle.php?article=' . $this->articleId . '&deleteComment=' . $comment->getId () . '">Delete comment </a><br/>';
			}
			echo '<strong>' . htmlspecialchars ( $comment->getUsername () ) . '</strong> wrote:<br/>';
			echo htmlspecialchars ( $comment->getContent () ) . '<br/><hr/>';
		}
	}
	public function renderAddComment() {
		if (parent::invokedByUser () || parent::invokedByAdmin ()) {
			
			echo '	<form action="" method="post"
				style="text-align: center; display: inline-block; margin: 0 auto;">
				<textarea rows="4" cols="50" name="comment"
						placeholder="Put your horrible comment here!" /></textarea>
						<br /> <input type="submit" value="C\'mon!" />
		
		
						</form> ';
		} else {
			
			echo '<p
			style="text-align: center; font-size: 14; color: red; vertical-align: middle; ">Only real dudes can post comments here. Wanna be one of them? Just login bro.</p>';
		}
	}
}

$view = new ReadArticleView ( $_GET ["article"] );
$view->render ();

